package com.shop.clientorder.chainduty.kernel;

import com.shop.clientorder.chainduty.Processor;
import com.shop.clientorder.chainduty.pojo.StockAttr;
import com.shop.common.core.domain.Result;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author：作者姓名
 * @Package：com.shop.clientorder.chainduty.kernel
 * @Project：e-commerce-services
 * @name：ProcessorChain
 * @Date：2024/7/8 8:49
 */
@Component
public class ProcessorChain {
    //保存处理结点
//    private List<Processor>processorList=new ArrayList<>();
    private List<Processor>processorList=new ArrayList<>();

    //处理节点下标
    private  int index=0;

    //动态扩展处理节点
    public ProcessorChain addProcessor(Processor processor){
        processorList.add(processor);
        return this;
    }

    // 获取处理器处理
    public Result<Boolean> process(StockAttr stockAttr, ProcessorChain chain) {
        if(index == processorList.size()) {
            return Result.success(true);
        }
        Processor processor = processorList.get(index);
        index++;
        Result<Boolean> process = processor.process(stockAttr, chain);
        return process;
    }
}
